<?php
namespace app\app\controller;

use alisms\Alidayu;
use app\common\controller\App;
use Common\utils\Alisms;
use extend\alisms\AlismsApi;
use think\captcha\Captcha;
use think\Config;
use think\console\command\optimize\Schema;
use think\console\command\Version;
use think\Db;
use think\Exception;
use think\route\Rule;

/**
 * 公共方法，发送短信、省市区三级联动等
 */
class Tiezi extends App
{

    protected $middleware = [
        'AppAuth' => ['only'=> ['fatie'] ],
    ];

    //社区帖子
    public function index()
    {

        $page=request()->param("page",1,'intval');
        $where=[];
        $where[]=['a.status','=',1];


        $user_id=$this->getLoginUid();
        $list=model("Tiezi")
             ->alias("a")
             ->leftJoin("Member b","a.uid=b.uid")
             ->order("a.id desc")
             ->page(1,12)
              ->where($where)
             ->field("a.*,b.realname")
             ->select();
        if (!empty($list)){
            foreach ($list as $key=>$val){
                $list[$key]['create_time']=date("Y-m-d H:i",$val['create_time']);
                $list[$key]['image_path']=model("File")->getPath($val['image_id']);
                $list[$key]['can_del']=0;
            }
        }
        $data=[
            'list'=>$list
        ];
        return $this->ok('请求成功',$data);

    }



    //帖子详情
    public function detail()
    {
        $id=request()->param("id",0,'intval');
        Db::name("Tiezi")->where(['id'=>$id])->setInc('look',1);
        $detail=model("Tiezi")
               ->alias("a")
               ->leftJoin("Member b","a.uid=b.uid")
               ->where(['a.id'=>$id,'a.status'=>1])
              ->field("a.*,b.realname,b.head_img")
              ->find();
        if (empty($detail)){
            return $this->fail('帖子不存在或已被管理员下架');
        }
        $detail['head_img']=model("File")->getPath($detail['head_img']);
        $detail['create_time']=date("Y年m月d日",$detail['create_time']);
        $detail['image_path']=model("File")->getPath($detail['image_id']);
        $detail['comment_count']=Db::name("Comment")->where(['pid'=>0,'tz_id'=>$id])->count();
        $comment=Db::name("Comment")
            ->alias("a")
            ->leftJoin("Member b","a.uid=b.uid")
            ->where(['a.tz_id'=>$id,'a.pid'=>0])
            ->order("a.id desc")
            ->field('a.*,b.realname,b.head_img')
            ->select();
        $uid=$this->getLoginUid();
        if (!empty($comment)){
            foreach ($comment as $key=>$val){
                $comment[$key]['create_time']=date("Y年m月d日",$val['create_time']);
                $comment[$key]['head_img']=model("File")->getPath($val['head_img']);
                $comment[$key]['is_my']=$val['uid']==$uid?1:0;
            }
        }
        $data=[
            'detail'=>$detail,
            'comment'=>$comment
        ];

        return $this->ok('请求成功',$data);

    }

    //发帖
    public function fatie()
    {
       $user_info=$this->getAppLoginInfo();
        $data['title']=request()->param("title");
        $data['content']=request()->param("content");
        $data['image_id']=request()->param("image_id",'0','intval');
        $data['uid']=$user_info['uid'];
        $data['house_id']=$user_info['house_id'];
        $data['create_time']=time();
        if (empty($data['title'])){
            return $this->fail('请填写标题');
        }
        if (empty($data['content'])){
            return $this->fail('请填写内容');
        }
        $res=model("Tiezi")->insert($data);
        if ($res){
            return $this->ok('发帖成功');
        }
        return $this->fail('发帖失败');
    }


    public function del()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        if (empty($uid)){
            return $this->fail('请先登录','','-99');
        }
        $res=Db::name("Tiezi")->where(['uid'=>$uid,'id'=>$id])->delete();
        if ($res){
            return $this->ok('删除成功');
        }else{
            return $this->fail('删除失败');
        }

    }
}
